<template>
  <a-card :bordered="false" class="card-area">
    <div class="table-page-search-wrapper">
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :sm="{span:24}" :md="{span:12}" :xl="{span:6}">
            <a-form-item label="院系">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.zsyxm" @change="yxChange">
                <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh+"-"+ i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col ::sm="{span:24}" :md="{span:12}" :xl="{span:6}">
            <a-form-item label="招生年度">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.zsnj" @change ="zsndChange">
                <a-select-option v-for="i in TYPE0126" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>

          <template v-if="advanced">
            <a-col ::sm="{span:24}" :md="{span:12}" :xl="{span:6}">
              <a-form-item label="专业">
                <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.zszym">
                  <a-select-option v-for="i in selectMenu.zy" :key="i.id" :value="i.zyh">{{i.zyh+'-'+ i.zymc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}">
              <a-form-item label="学习形式">
                <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.qrzhfqrz">
                  <a-select-option v-for="i in selectMenu.xxlx" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :sm="{span:16}" :xl="{span:8}">
            <span class="table-page-search-submitButtons" >
            <a-button type="primary" @click="handleReport">还原未上报状态</a-button>
            <a-button v-hasPermission="'fpzyzsjh:add'" type="primary" @click="handleCreate">添加</a-button>
            <a-button type="primary" @click="loadData">查询</a-button>
            <a-button type="primary" @click="exportExcel">导出</a-button>
            <a-button type="primary" @click="exportDbf">导出DBF</a-button>
            <a @click="advanced = !advanced" style="margin-left: 8px">
              {{ advanced ? '收起' : '展开' }}
              <a-icon :type="advanced ? 'up' : 'down'" />
            </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <a-table ref="table" size="default" rowKey="key" :columns="columns" :loading="loading" :dataSource="data" :alert="options.alert"
              :pagination="pagination" @change="handleTableChange" :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}">
      <template slot="action" slot-scope="text,record">
        <a v-hasPermission="'fpzyzsjh:update'" @click="handleEdit(record.id)">修改</a>
        <a v-hasPermission="'fpzyzsjh:delete'" @click="handleDelete(record.id)">删除</a>
      </template>
      <span slot="serial" slot-scope="text, record, index">
        {{ index + 1 }}
      </span>
    </a-table>
    <!-- 新增 -->
    <office-add ref="officeAdd" @close="handleCreateClose" @success="handleCreateSuccess"
                      :officeAddVisiable="officeAddVisiable"></office-add>
    <!-- 编辑 -->
    <office-edit ref="officeEdit" @close="handleEditClose" @success="handleEditSuccess" :info="officeEditData"
                       :officeEditVisiable="officeEditVisiable"></office-edit>
  </a-card>
</template>

<script>
  // import moment from 'moment'
  import OfficeAdd from './officeAdd'
  import OfficeEdit from './officeEdit'
  import {
    STable
  } from '~'
  import {
    mapState
  } from 'vuex'

  const selectArr = ['TYPE0311', 'TYPE0312', 'TYPE0126', 'TYPE0075', 'TYPE0056', 'TYPE0083', 'TYPE0125', 'TYPE0072']
  export default {
    name: 'Office',
    components: {
      STable,
      OfficeAdd,
      OfficeEdit
    },
    data() {
      return {
        advanced: true,
        loading: false,
        data: [],
        // 查询参数
        queryParam: {
          zsjhlxm: '02',
          zsnj: '',
          zsyxm: '',
          qrzhfqrz: '',
          zszym: ''
        },
        upStatus : true,
        postParams: {},
        // 表头
        columns: [{
          title: '院系编码',
          dataIndex: 'zsyxm'
        },
          {
            title: '院系',
            dataIndex: 'dwmc'
          },
          {
            title: '招生年度',
            dataIndex: 'zsnj'
          },
          {
            title: '专业代码',
            dataIndex: 'zszym'
          },
          {
            title: '专业名称',
            dataIndex: 'zymc'
          },
          {
            title: '总计划',
            dataIndex: 'zszjhrs'
          },
          {
            title: '培养模式',
            dataIndex: 'pymsmc'
          },
          {
            title: '学习形式',
            dataIndex: 'qrzhfqrz'
          },
          {
            title: '操作',
            dataIndex: 'cz',
            fixed: 'right',
            width: 120,
            scopedSlots: {
              customRender: 'action'
            }
          }
        ],
        selectedRowKeys:[],
        selectedRows:[],
        // custom table alert & rowSelection
        options: {
          alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
          rowSelection: {
            selectedRowKeys: this.selectedRowKeys,
            onChange: (selectedRowKeys, selectedRows) => {
              console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows)
              this.selectedRowKeys = selectedRowKeys
              this.selectedRows = selectedRows
            },
            onSelect: (record, selected, selectedRows) => {
              console.log(record, selected, selectedRows)
            },
            onSelectAll: (selected, selectedRows, changeRows) => {
              console.log(selected, selectedRows, changeRows)
            },
          }
        },
        visible: false,
        confirmLoading: false,
        editId: null,
        selectMenu: {
          yx: [], // 院系
          zy: [], // 所属专业
          xxlx: [] //学习形式
        },
        officeAddVisiable: false,
        officeEditVisiable: false,
        officeEditData: {
          hzfs: '',
          zjh: '',
          fzyxwjh: '',
          zyxwjh: ''
        },
        pagination: {
          current: 1
        }
      }
    },
    computed: {
      ...mapState({
        systemInfo: state => state.app.systemInfo,
        TYPE0126: state => state.app['TYPE0126'] // 年级
      })
    },
    created() {

      selectArr.forEach(v => {
        this.$store.dispatch('app/setDictionary', v)
      })
      this.$api.recruitstat.masterPlanGetTime().then(res => {
        let d1=res.data.qssj._i;
        d1 = new Date(Date.parse(d1))
        let d2=res.data.jssj._i;
        d2 = new Date(Date.parse(d2))
        let d3=new Date();
        if(d3<d1 || d3>d2){
          this.$message.warning("当前时间不在分配专业招生计划时间范围内!")
          return false
        }else{
          this.initSelectMenu()
        }
      })
    },
    methods: {
      initSelectMenu() {
        let allRequest = [
          this.$api.base.collegeAll()
        ]
        Promise.all(allRequest).then(res => {
          this.selectMenu.yx = res[0].data
          this.queryParam.zsyxm = res[0].data[0].dwh
          this.getProssion(this.queryParam.zsyxm)
          // 获取学习形式
          this.$api.base.dictionaryData('TYPE0441').then(res => {
            this.selectMenu.xxlx = res.data
          })
          // 获取当前学年
          this.$api.base.getSchoolYear().then(res => {
            this.queryParam.zsnj = res.data.masterAdmissionYear
            this.loadData({
              pageSize: 10,
              pageNum: 1
            })

          })
        })
      },
      // 选择院系
      yxChange(code) {
        console.log(code)
        // this.postParams.zsyxm = code
        // var yx = this.selectMenu.yx
        // var xxlx = this.selectMenu.xxlx
        this.$set(this.queryParam, 'zszym', '')
        this.$set(this.selectMenu,'zy',[])
        if(code){
          this.getProssion(code)

        }
      },
      getProssion(code) {
        this.$api.base.majorAll({
          lsdwh: code
        }).then(res => {
          if (res.data) {
            this.selectMenu.zy = res.data
          }
        })
      },
      // 选择招生年度
      zsndChange(code) {

      },
      // 多选
      onSelectChange (selectedRowKeys,selectedRows) {
        console.log('selectedRowKeys changed: ', selectedRowKeys);
        this.selectedRows = selectedRows
        this.selectedRowKeys = selectedRowKeys
      },
      // 加载数据
      loadData(parameter = {}) {
        if(!parameter.pageNum){
          this.pagination.current = 1
          parameter.pageNum = 1
        }
        let params = Object.assign(this.queryParam, parameter)
        this.loading = true;
        this.$api.recruitstat.disProsseionOfficeList(params).then(res => {
          // console.log(res)
          const pagination = { ...this.pagination
          };
          pagination.total = 0
          if (res.data && res.data.total) {
            pagination.total = res.data.total;
          }
          this.data = []
          this.pagination = pagination;
          if (res.data && res.data.rows.length) {
            res.data.rows.map((item, index) => {
              this.selectMenu.xxlx.map((_item, _index) => {
                if (item.qrzhfqrz == _item.code) {
                  item.qrzhfqrzmz = _item.name
                }
              })
              return item;
            })
            this.data = res.data.rows;
          }
          this.loading = false;
        })
      },
      // 分页
      handleTableChange(pagination, filters, sorter) {
        console.log(pagination);
        const pager = { ...this.pagination
        };
        pager.current = pagination.current;
        this.pagination = pager;
        this.loadData({
          pageSize: 10,
          pageNum: pagination.current
        });
      },
      // 导出
      exportExcel() {
        console.log('导出')
        this.$export('admissions/zsbfpzyzsjh/exportZsjh', this.queryParam)
      },
      exportDbf() {
        console.log('导出')
        this.$export('admissions/zsbfpzyzsjh/exportdbf', this.queryParam)
      },
      // 添加
      handleCreate() {
        this.officeAddVisiable = true
      },
      // 关闭添加弹层
      handleCreateClose() {
        this.officeAddVisiable = false
      },
      // 添加成功
      handleCreateSuccess() {
        this.officeAddVisiable = false
        this.$message.success('添加成功')
        this.loadData({
          pageSize: 10,
          pageNum: 1
        })
      },
      // 修改
      handleEdit(key) {
        this.$api.recruitstat.disProsseionOfficeId(key).then(res => {
          this.officeEditVisiable = true
          if (res.data) {
            this.officeEditData = res.data
          }
        })
      },
      // 关闭添加弹层
      handleEditClose() {
        this.officeEditVisiable = false
      },
      // 删除
      handleDelete (key) {
        let _this=this;
        this.$confirm({
          title: '确定删除?',
          onOk() {
            _this.$api.recruitstat.disProsseionOfficeDelete(key).then(res=>{
              if(res.status==1){
                _this.$message.success(res.msg)
                _this.loadData({
                  pageSize:10,
                  pageNum:_this.pagination.current
                })
              }else{
                _this.$message.warning(res.msg)
              }
            })
          },
          onCancel() {
            console.log('Cancel');
          },
          class: 'test',
        });

      },
      // 添加成功
      handleEditSuccess() {
        this.officeEditVisiable = false
        this.$message.success('修改成功')
        this.loadData({
          pageSize: 10,
          pageNum: this.pagination.current
        })
      },
      // 上报
      handleReport () {
        var that = this;
        this.$confirm({
          title: '确定还原未上报?',
          onOk() {
            console.log(that.selectedRows);
            let ids = ''
            that.selectedRows.forEach((item,index) =>{
              if(item.id){
                ids += item.id+(index+1 == that.selectedRows.length? '':',')
              }
            })
            let params = 'ids='+ids
            console.log(params)
            that.$api.recruitstat.disProsseionOfficeUp(params).then(res => {
              that.loadData({
                pageSize:10,
                pageNum:that.pagination.current
              })
              that.selectedRowKeys=[]
            })
          },
          onCancel() {

          },
          class: 'test',
        });
      },

    }
  }
</script>
